package com.itwn.email.consumer.g_dlx;

import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

//@Component
public class DlxListener {

    /**
     * 消息确认、消费者可靠性保证
     */
    @SneakyThrows
    @RabbitListener(queues = "normal.queue")
    public void handle(String content, Channel channel, Message message) {
        try {
            System.out.println("content = " + content);
            int i = 4 / 0;
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            Boolean redelivered = message.getMessageProperties().getRedelivered();
            if (!redelivered) {
                channel.basicNack(
                        message.getMessageProperties().getDeliveryTag(),false,true);
            } else {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            }
        }
    }
}